package com.hackerrank.challenges.angrychildren;

import java.util.Arrays;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt(), K = in.nextInt(), x[] = new int[N], m = Integer.MAX_VALUE;
		for(int i = 0; i < N; i++)
			x[i] = in.nextInt();
		Arrays.sort(x);
		for(int i = 0; i <= N - K; i++)
			m = Math.min(m, x[i + K - 1] - x[i]);
		System.out.println(m);
	}
}
